
package br.org.underserver.resquisicao;

import br.org.underserver.servidor.ServerHTTP;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.net.URLDecoder;
/**
 * @author alexandre.araujo
 */
public class Request {
   private String method;
	private String uri;
	private String protocol;
        private Map<String, String[]> parameters;
	public Request() {
	}

	/**
	 * Realiza o parse
	 * 
	 * @param input
	 * @return
	 * @throws IOException
	 */
	public void parse(String input) throws IOException {
		BufferedReader br = new BufferedReader(new StringReader(input));
		String line = null;
		int lineNumber = 0;
                boolean achoup = false;
		while ((line = br.readLine()) != null) {
			//System.out.println(" " + line);
			if (lineNumber == 0){
				String[] values = line.split(" ");
				if (values.length == 3) { 
					this.method = values[0];
                                        this.uri = values[1];
                                        this.protocol = values[2];
                                        if(this.method.equals("GET")){
                                            this.parseMapParameters(uri);
                                            achoup=true;
                                        }                                       
				}// TODO Tratar erro
                               
			}
                        
                        if(achoup){
                            break;
                        }else if(line.contains("idRequest=")){
                            //System.out.println("ACHADO " + line);
                            this.parseMapParameters("/?"+line);
                            break;
                        }
                        
                        
                        /*else {
				// TODO Recuperar os cabeçalhos e corpo
			}
			lineNumber++;
                                 
                                 */
                        lineNumber++;

		}
	}

	/**
	 * Recupera o método
	 * 
	 * @return metodo TODO: Converter para Enum
	 */
	public String getMethod() {
		return method;
	}

	/**
	 * Recupera a URI
	 * 
	 * @return uri
	 */
	public String getUri() {
		return uri;
	}

	/**
	 * Recupera o protocolo
	 * 
	 * @return TODO: Fazer validação
	 */
	public String getProtocol() {
		return protocol;
	}
        private void parseMapParameters(String urli){
            int achado = urli.indexOf("?");
            //System.out.println("-->params:"+urli); 
            if(achado>0){
                String urlmicroafter = urli.substring(achado,urli.length());
            try {
                
                urlmicroafter = URLDecoder.decode(urlmicroafter,"UTF-8");
            } catch (Exception ex) {
               ServerHTTP.addInfo(ex.getMessage().toString());
            }
                this.parameters = new HashMap<String, String[]>();
                Pattern pa = Pattern.compile("[?&](.*?)=([^&#]*)");
                Matcher m = pa.matcher(urlmicroafter);
                        while (m.find()) {
                                // chamada diferente:
                                System.out.println("-->param:"+m.group(1)+" valor:"+m.group(2));            
                                String[] p = {m.group(2)};                
                                this.parameters.put(m.group(1),p);
                        } 
                }
        }
        public Map<String, String[]> getParameters() {
            return parameters;
        }
}
